<?php
if (! defined('IS_INITPHP'))
    exit('Access Denied!');
require_once ("qiniu/rs.php");

/**
 * 七牛的类封装，包括上传的授权token获取，私有资源URL的获取以及文件上传
 *
 * @author Chen Deben
 * @copyright Meitu
 */
class qiniuInit {

    private $APP_ACCESS_KEY;

    private $APP_SECRET_KEY;

    private $DOMAIN;

    private $BUCKET;

    private $CALLBACK;

    public function __construct() {
        $config = InitPHP::getConfig();
        $config = $config['qiniu'];
        $this->DOMAIN = $config['domain'];
        $this->APP_ACCESS_KEY = $config['access_key'];
        $this->APP_SECRET_KEY = $config['secret_key'];
        $this->BUCKET = $config['bucket'];
        $this->CALLBACK = $config['callback'];
        Qiniu_SetKeys($this->APP_ACCESS_KEY, $this->APP_SECRET_KEY);
    }

    /**
     * 获取七牛Token(不需要回调)
     *
     * @param $uid string
     *            用户的uid
     * @param $offline_result string
     *            离线点数据
     * @return string
     */
    public function get_upload_token_nocall($uid) {
        $uinqid = uniqid() . mt_rand(1, 10000);
        $key = $uid . "/" . $uinqid . '.jpg';
        $params = array(
            'expires_in' => 2592000
        ) // 直传七牛token过期时间(s)
;
        $putPolicy = new Qiniu_RS_PutPolicy($this->BUCKET . ":" . $key, $params);
        $upload_token = $putPolicy->Token(null);
        if (! $upload_token) {
            // 获取七牛直传token失败时返回给客户端
            $return_data = array(
                'upload_token' => ''
            );
        } else {
            $return_data = array(
                'upload_token' => $upload_token,
                'key' => $key,
                'expires_at' => 2592000 + time()
            );
        }
        return $return_data;
    }

    /**
     * 获取七牛Token 游客使用，不需要回调
     *
     * @param string $token            
     * @param string $offline_result            
     * @return Ambigous <multitype:string , multitype:number string >
     */
    public function get_upload_token_by_nouid_nocall($token) {
        $uinqid = uniqid() . mt_rand(1, 10000);
        $key = "guests/" . $token . "/" . $uinqid . '.jpg';
        $params = array(
            'expires_in' => 2592000
        );
        
        $putPolicy = new Qiniu_RS_PutPolicy($this->BUCKET . ":" . $key, $params);
        $upload_token = $putPolicy->Token(null);
        if (! $upload_token) {
            // 获取七牛直传token失败时返回给客户端
            $return_data = array(
                'upload_token' => ''
            );
        } else {
            $return_data = array(
                'upload_token' => $upload_token,
                'key' => $key,
                'expires_at' => 2592000 + time()
            );
        }
        return $return_data;
    }
    /**
     * 获取七牛Token(无法识别人脸)
     *
     * @param $uid string
     *            用户的uid
     * @param $offline_result string
     *            离线点数据
     * @return string
     */
    public function get_upload_noface_token($uid, $access_token,$log='') {
        $uinqid = uniqid() . mt_rand(1, 10000);
        $key = "noface/" . $uid . "/" . $uinqid . '.jpg';
        $params = array(
            'expires_in' => 2592000, // 直传七牛token过期时间(s)
            'callback_url' => $this->CALLBACK, // 客户端直传后回调地址（七牛引导过来的）
            'callback_body' => 'url=' . $key . '&log='.$log.'&uid=' . $uid . '&access_token=' . $access_token
        );
        $putPolicy = new Qiniu_RS_PutPolicy($this->BUCKET . ":" . $key, $params);
        $upload_token = $putPolicy->Token(null);
        if (! $upload_token) {
            // 获取七牛直传token失败时返回给客户端
            $return_data = array(
                'upload_token' => ''
            );
        } else {
            $return_data = array(
                'upload_token' => $upload_token,
                'key' => $key,
                'expires_at' => 2592000 + time()
            );
        }
        return $return_data;
    }
    /**
     * 获取七牛Token(无法识别人脸，游客使用)
     *
     * @param string $token            
     * @param string $offline_result            
     * @return Ambigous <multitype:string , multitype:number string >
     */
    public function get_upload_token_noface_by_guest($token, $timestamp, $secret,$log="") {
        $uinqid = uniqid() . mt_rand(1, 10000);
        $key = "noface/guests/" . $token . "/" . $uinqid . '.jpg';
        $params = array(
            'expires_in' => 2592000,
            'callback_url' => $this->CALLBACK, // 客户端直传后回调地址（七牛引导过来的）
            'callback_body' => 'url=' . $key . '&log='.$log.'&token=' . $token . '&timestamp=' . $timestamp . '&secret=' . $secret
        ); // 直传七牛token过期时间(s)
        
        $putPolicy = new Qiniu_RS_PutPolicy($this->BUCKET . ":" . $key, $params);
        $upload_token = $putPolicy->Token(null);
        if (! $upload_token) {
            // 获取七牛直传token失败时返回给客户端
            $return_data = array(
                'upload_token' => ''
            );
        } else {
            $return_data = array(
                'upload_token' => $upload_token,
                'key' => $key,
                'expires_at' => 2592000 + time()
            );
        }
        return $return_data;
    }
    /**
     * 获取私有资源的URL
     *
     * @param unknown $key
     * @return string
     */
    public function getUrl($key) {
        $baseUrl = Qiniu_RS_MakeBaseUrl($this->DOMAIN, $key);
        $getPolicy = new Qiniu_RS_GetPolicy();
        $privateUrl = $getPolicy->MakeRequest($baseUrl, null);
        return $privateUrl;
    }
}